/* Bazinga - MPD Jukebox
 * Copyright (C) 2010 m3Soft (Martin Sieke, Matthias Sieke, Matthias Bockstegers)
 * 
 * This program is distributed under the terms of the GNU General Public License.
 * You can redistribute  and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program.  
 * If not, see http://www.gnu.org/licenses
 */

package de.m3soft.bazinga;

import org.bff.javampd.MPDPlayer;
import org.bff.javampd.events.PlayerBasicChangeEvent;
import org.bff.javampd.events.PlayerBasicChangeListener;
import org.bff.javampd.events.PlaylistBasicChangeEvent;
import org.bff.javampd.events.PlaylistBasicChangeListener;
import org.bff.javampd.events.PlaylistChangeEvent;
import org.bff.javampd.events.PlaylistChangeListener;
import org.bff.javampd.events.TrackPositionChangeEvent;
import org.bff.javampd.events.TrackPositionChangeListener;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDDatabaseException;
import org.bff.javampd.exception.MPDException;
import org.bff.javampd.exception.MPDPlaylistException;
import org.bff.javampd.monitor.MPDStandAloneMonitor;
import org.bff.javampd.objects.MPDSong;

public class Listener implements TrackPositionChangeListener, PlayerBasicChangeListener, PlaylistBasicChangeListener, PlaylistChangeListener {

	private BazingaGUI myGUI;

	private Methoden m;

	Listener(BazingaGUI gui, Methoden m) {
		this.myGUI = gui;
		this.m = m;
		MPDStandAloneMonitor mpdStandAloneMonitor = new MPDStandAloneMonitor(m.getMPD(), 2000);
		mpdStandAloneMonitor.addTrackPositionChangeListener(this);
		mpdStandAloneMonitor.addPlayerChangeListener(this);
		mpdStandAloneMonitor.addPlaylistChangeListener(this);
		Thread th = new Thread(mpdStandAloneMonitor);
		th.start();
	}

	public void trackPositionChanged(TrackPositionChangeEvent event) {
		try {
			m.removePlayed();
			MPDSong currentPlaying = m.getPlayList().getCurrentSong();

			if (m.getPlayer().getStatus().equals(MPDPlayer.PlayerStatus.STATUS_PLAYING)) {
				if (event.getElapsedTime() <= 5) {
					myGUI.updatePlaylist();
					myGUI.updateState(currentPlaying);
					try {
						myGUI.updateAlbumArt(m.getArtist(currentPlaying).getName(), m.getAlbum(currentPlaying).getName());
					} catch (NullPointerException e) {
						myGUI.updateAlbumArt("", "");
					}
					myGUI.notification("", "", "playing");
				}
				myGUI.setRemain(currentPlaying.getLength(), event.getElapsedTime());
				if ((m.getPlaylistLength() <= 1) && (currentPlaying.getLength() - event.getElapsedTime() < 100)) {
					m.addRandomSong();
				}
			} else if (m.getPlaylistLength() == 0) {
				m.addRandomSong();
				m.getPlayer().play();
			}

			if (m.getPlayer().getElapsedTime() >= Integer.parseInt(m.getProperty("maxTrackTime"))) {
				m.skip();
			}
		} catch (MPDPlaylistException e) {
		} catch (MPDConnectionException e) {
		} catch (MPDDatabaseException e) {
		} catch (MPDException e) {
		}

	}

	public void playerBasicChange(PlayerBasicChangeEvent event) {
		// System.out.println("Player event received:"+event.getId());
		switch (event.getId()) {
		case (PlayerBasicChangeEvent.PLAYER_STARTED):
			myGUI.setConsole("The player was started!");
			break;
		case (PlayerBasicChangeEvent.PLAYER_STOPPED):
			myGUI.setConsole("The player was stopped!");
			break;
		}
	}

	public void playlistBasicChange(PlaylistBasicChangeEvent event) {
		switch (event.getId()) {
		case (PlaylistBasicChangeEvent.PLAYLIST_CHANGED):
			try {
				myGUI.updatePlaylist();
			} catch (MPDPlaylistException e) {
				myGUI.notification("Playlist error", "Error fetching playlist");
			} catch (MPDConnectionException e) {
				myGUI.notification("Connection error", "Error fetching playlist from MPD");
			}
			return;
		}
	}

	public void playlistChanged(PlaylistChangeEvent arg0) {
		try {
			myGUI.updatePlaylist();
		} catch (MPDPlaylistException e) {
			myGUI.notification("Playlist error", "Error fetching playlist");
		} catch (MPDConnectionException e) {
			myGUI.notification("Connection error", "Error fetching playlist from MPD");
		}
	}
}
